package com.meatball.game.util

import com.meatball.game.service.AiGameService
import org.slf4j.LoggerFactory
import org.springframework.data.redis.connection.Message
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener
import org.springframework.data.redis.listener.RedisMessageListenerContainer
import org.springframework.stereotype.Component

@Component
class RedisExpiredKeyListener(
        private val aiGameService: AiGameService,
        redisMessageListenerContainer: RedisMessageListenerContainer
) : KeyExpirationEventMessageListener(redisMessageListenerContainer) {
    companion object {
        private val log = LoggerFactory.getLogger(RedisExpiredKeyListener::class.java)
    }

    override fun onMessage(message: Message, pattern: ByteArray?) {
        val expiredKey = String(message.body)
        log.info("redisExKeyOnMessage|redis过期key监听start|{}", expiredKey)
        if (expiredKey.startsWith(COUNTDOWN)) {
            aiGameService.expirationKey(expiredKey)
        }
        if (expiredKey.startsWith(HALF_TIME)) {
            aiGameService.halfTime(expiredKey)
        }
    }
}